﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HaiHui.Business;
using HaiHui.Data;

namespace HaiHui
{
    /// <summary>
    /// Interaction logic for Albums.xaml
    /// </summary>
    public partial class Albums : UserControl
    {
        private event EventHandler<GenericEventArgs<Album>> m_AlbumChanged;
        public event EventHandler<GenericEventArgs<Album>> AlbumChanged
        {
            add
            {
                m_AlbumChanged += value;
            }
            remove
            {
                m_AlbumChanged -= value;
            }
        }

        private List<Album> m_Albums;

        public Albums()
        {
            InitializeComponent();

            lbAlbumList.ItemsSource = DataStore.Albums;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void lbAlbumList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0)
            {
                //no albums selected
                m_AlbumChanged(this, new GenericEventArgs<Album>(null));
                
            }
            else
            {
                m_AlbumChanged(this, new GenericEventArgs<Album>((Album)e.AddedItems[0]));
            }
        }

        private void btnAddAlbum_Click(object sender, RoutedEventArgs e)
        {
            AlbumEditForm aef = new AlbumEditForm(null);
            aef.ShowDialog();
        }

        private void mnuEditAlbum_Click(object sender, RoutedEventArgs e)
        {
            EditAlbum();
        }

        private void btnEditAlbum_Click(object sender, RoutedEventArgs e)
        {
            EditAlbum();
        }

        private void EditAlbum()
        {
            if (lbAlbumList.SelectedItems.Count == 1)
            {
                Album album = (Album)lbAlbumList.SelectedItems[0];
                AlbumEditForm aef = new AlbumEditForm(album);
                if (aef.ShowDialog() == true)
                {
                    DataStore.LoadAlbumUpdates();
                }
            }
        }
    }

    public class GenericEventArgs<T> : EventArgs
    {
        private T m_Data;

        public T Data
        {
            get { return m_Data; }
        }

        public GenericEventArgs(T data)
        {
            m_Data = data;
        }
    }

}
